﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenExpressApp.WPF.Controls;
using System.Collections;
using Csla;
using Csla.Core;
using System.Windows;
using System.Collections.ObjectModel;
using Ricciolo.Controls;
using System.Windows.Controls;
using System.Windows.Data;
using System.ComponentModel;
using OpenExpressApp.Types;
using System.Windows.Input;
using Hardcodet.Wpf.GenericTreeView;

namespace OpenExpressApp.Module.WPF.Editors
{
    /// <summary>
    /// 树型的列表编辑器
    /// </summary>
    public class ObjectTreeEditor : ListEditor
    {
        /// <summary>
        /// 使用的控件
        /// </summary>
        private MultiObjectTreeView _treeView;

        public ObjectTreeEditor(IListObjectView listview, Type boType)
            : base(listview, boType) { }

        public override object CurrentObject
        {
            get
            {
                return this._treeView.SelectedItem;
            }
            set
            {
                //if (value is ITreeNode)
                //{
                //    this._treeView.EnsureNodeLoaded(value as BusinessBase);
                //}
                this._treeView.SelectedItem = value as BusinessBase;
            }
        }

        public override IList SelectedObjects
        {
            get
            {
                return this._treeView.SelectedItems;
            }
        }

        public override void SelectAll()
        {
            this._treeView.SelectAll();
        }

        public override void NotifyViewDataChanged()
        {
            this._treeView.SelectedItemChanged -= TreeView_SelectedItemChanged;
            this._treeView.ItemsSource = this.View.Data as IList;
            this._treeView.SelectedItemChanged += TreeView_SelectedItemChanged;
            this._treeView.IsReadOnly = this.IsReadOnly;
        }

        /// <summary>
        /// 根据rootPid绑定数据
        /// </summary>
        /// <param name="rootPid">
        /// 如果这个值不是null，则这个值表示绑定的所有根节点的父id。
        /// </param>
        public void BindData(object rootPid)
        {
            this._treeView.SelectedItemChanged -= TreeView_SelectedItemChanged;
            this._treeView.BindData(rootPid);
            this._treeView.SelectedItemChanged += TreeView_SelectedItemChanged;
        }

        protected override FrameworkElement CreateControl()
        {
            this._treeView = AutoUI.UIFactory.CreateTreeListControl(this.View);
            this._treeView.SelectedItemChanged += TreeView_SelectedItemChanged;

            var listener = this.View as IEventListener;
            if (listener != null)
            {
                this._treeView.MouseDoubleClick += listener.NotifyMouseDoubleClick;
            }

            return this._treeView;
        }

        private void TreeView_SelectedItemChanged(object sender, RoutedTreeItemEventArgs<BusinessBase> e)
        {
            if (this._treeView.IsReadOnly == false)
            {
                var tv = this._treeView.Tree as TreeListView;

                //如果切换列表数据时ObjectTreeViewItems重新装载，这时找不到匹配e.OldItem项
                IBindingList list = this.View.Data as IBindingList;
                if (list != null && list.AllowEdit)
                {
                    var items = this._treeView.ObjectItems;
                    TreeListViewItem item = null;
                    //object id = null;
                    //当改变Items时
                    if (e.OldItem != null)
                    {
                        var key = MultiObjectTreeView.GetTreeId(e.OldItem);
                        if (items.TryGetValue(key, out item))
                        {
                            item.UpdateContentTemplate(false);
                        }
                    }
                    if (e.NewItem != null)
                    {
                        var key = MultiObjectTreeView.GetTreeId(e.NewItem);
                        if (items.TryGetValue(key, out item))
                        {
                            item.UpdateContentTemplate(true);
                        }
                    }
                }
            }

            //通知view
            var listener = this.View as IEventListener;
            if (listener != null)
            {
                var args = new SelectedItemChangedEventArgs(e.NewItem, e.OldItem);
                listener.NotifySelectedItemChanged(sender, args);
            }

            e.Handled = true;
        }
    }
}